package com.reptile.science.utils;

import com.reptile.science.model.Science;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Html;
import us.codecraft.webmagic.selector.Selectable;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class ReptileUtils  implements PageProcessor {

    private static String URL = "https://apps.webofknowledge.com/Search.do?product=UA&SID=5AIW8XZ2Ogr7HGUCKNf&search_mode=GeneralSearch&prID=7e8dae73-f323-49ac-9756-08576e0b3686";

    private static String agent = "Mozilla/5.0 (Linux; U; Android 2.2.1; zh-cn; HTC_Wildfire_A3333 Build/FRG83D) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1";

    @Override
    public void process(Page page) {
        List<Selectable> nodes = page.getHtml().$("div.search-results div.search-results-content").nodes();
        if(nodes.isEmpty()){
            //如果为空，表示这是文献信息详情页 保存文献详情
            try{
                this.saveScienceText(page);
            }catch (Exception e){
                e.printStackTrace();
            }
        }else{
            for(Selectable node : nodes){
                //如果有值 就表示是文献信息列表页
                String textUrl = node.links().toString();
                //添加到url列表
                page.addTargetRequest(textUrl);
                //获取翻页按钮的URL
                List<String> listUrl = page.getHtml().$("form#summary_navigation a.snowplow-navigation-nextpage-bottom").links().all();
                //添加到任务列表
                page.addTargetRequests(listUrl);
            }
        }
    }

    private Site site = Site
            .me()
            .setDomain(URL)
            .setSleepTime(10)
            .setUserAgent(agent);

    @Override
    public Site getSite() {
        return site;
    }


    int i = 0;


    public boolean isUpper(String word){
        for(int i = 0; i < word.length(); i++) {
            char c = word.charAt(i);
            if (Character.isLowerCase(c)) {
                return false;
            }
        }
        return true;
    }

    /**
     * 保存数据
     */
    private void saveScienceText(Page page){
        Science science = new Science();
        Html html = page.getHtml();
        ArrayList<String> alist = new ArrayList<>();
        //获取内容
        science.setTi(html.$("div.l-content div.title value","text").toString());
        //作者
        String AU = html.xpath("//div[@class='l-content']/div[2]//p[@class='FR_field']//a/text()").nodes().toString().replaceAll(", ,","\r\n").replaceAll("\\[","").replaceAll("]","");
        //判断作者属于哪种形式
        if("".equals(AU.replaceAll(" ",""))){
           AU = html.xpath("//div[@class='l-content']/div[2]//p[@class='FR_field']//fullname/text()").nodes().toString().replaceAll(", ,","\r\n").replaceAll("\\[","").replaceAll("]","");
        }
        science.setAu(AU);
        //作者全称
        List<Selectable> AFNodes = html.xpath("//div[@class='block-record-info']//p[@class='FR_field']/text()").nodes();
        science.setAf(AFNodes.get(0).toString().replaceAll("\\(","").replaceAll("\\)","").replaceAll(";","\r\n"));
        //出版物来源
        science.setSo(html.xpath("//div[@class='block-record-info block-record-info-source']//span[@class='sourceTitle']/value/text()").toString());
        List<String> DI = html.xpath("//div[@class='l-content']//div[@class='block-record-info block-record-info-source']//p/value/text()").all();
        for(String str : DI){
            if(str.contains("/")){
                science.setDi(str);
                break;
            }
        }

        i++;
        System.out.println(i);
    }

    public static void Agent(){
        Random r = new Random(0);
        int ran = r.nextInt(9);
        switch (ran){
            case 1 :
                agent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36 OPR/26.0.1656.60";
                break;
            case 2 :
                agent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.57.2 (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2";
                break;
            case 3 :
                agent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36";
                break;
            case 4 :
                agent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.133 Safari/534.16";
                break;
            case 5 :
                agent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36";
                break;
            case 6 :
                agent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.11 TaoBrowser/2.0 Safari/536.11";
                break;
            case 7 :
                agent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; QQBrowser/7.0.3698.400)";
                break;
            case 8 :
                agent = "Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5";
                break;
            case 9 :
                agent = "Mozilla/5.0 (Linux; U; Android 2.2.1; zh-cn; HTC_Wildfire_A3333 Build/FRG83D) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1";
                break;
        }

    }

    public static void main(String[] args) {
//        Agent();
        Spider.create(new ReptileUtils())
                .addUrl(URL)
                .thread(1)
                .run();
    }
}
